/*
* Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
* 
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Eclipse Public License
* Version 1.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://opensource.org/licenses/EPL-1.0 and read it before using this file.
*/

#include "stdafx.h"
#include "CScriptableImage.h"
#include "variant.h"
#include "scripting.h"


CScriptableImage::CScriptableImage()
    : m_item( NULL ),
    m_scriptableControl( NULL )
{
}

CScriptableImage::~CScriptableImage()
{
}

STDMETHODIMP CScriptableImage::get_width( VARIANT* pVal )
{
    if ( m_item )
    {
        pVal->vt = VT_INT;
        pVal->intVal = m_item->getWidth();
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::get_height( VARIANT* pVal )
{
    if ( m_item )
    {
        pVal->vt = VT_INT;
        pVal->intVal = m_item->getHeight();
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::get_pixelType( VARIANT* pVal )
{
    if ( m_item )
    {
        pVal->vt = VT_INT;
        pVal->intVal = ( int ) m_item->getPixelType();
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::get_resolution( VARIANT* pVal )
{
    if ( m_item )
    {
        pVal->vt = VT_INT;
        pVal->intVal = m_item->getResolution();
    }
    return S_OK;
}

void CScriptableImage::disposed()
{
    m_item = NULL;
}

STDMETHODIMP CScriptableImage::getImageData( VARIANT* pX, VARIANT* pY, VARIANT* pWidth, VARIANT* pHeight, VARIANT* pResult )
{
    pResult->vt = VT_NULL;
    pResult->pdispVal = NULL;

    DWORD t = ::GetTickCount();
    DWORD t2 = 0;
    if ( ::varIsNumeric( pX ) && ::varIsNumeric( pY ) && ::varIsNumeric( pWidth ) && ::varIsNumeric( pHeight ) )
    {
        int x = ::varAsNumeric<int>( pX ), y = ::varAsNumeric<int>( pY ), width = ::varAsNumeric<int>( pWidth ), height = ::varAsNumeric<int>( pHeight );

        if ( x >= 0 || y >= 0 || width > 0 || height > 0 )
        {
            List<byte_t> list;

            if ( m_item->getImageData( list, x, y, width, height ) )
            {
                t2 = ::GetTickCount() - t;
                IDispatch* disp = ::listToArray( m_scriptableControl->m_browser, list );
                pResult->vt = VT_DISPATCH;
                pResult->pdispVal = disp;
            }
        }
    }

    DWORD t3 = ::GetTickCount() - t;

    return S_OK;
}

STDMETHODIMP CScriptableImage::getThumbnailUrl( VARIANT* pResult )
{
    const char* dataUrl = m_item->getThumbnailUrl();

    if ( dataUrl )
    {
        BSTR bstrDataUrl = ::charToBstr( dataUrl );
        pResult->vt = VT_BSTR;
        pResult->bstrVal = bstrDataUrl;
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::get_index( VARIANT* pVal )
{
    if ( m_item )
    {
        pVal->vt = VT_INT;
        pVal->intVal = m_item->getIndex();
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::getPixel( VARIANT* pX, VARIANT* pY, VARIANT* pResult )
{
    if ( m_item )
    {
        int x = ::varAsNumeric<int>( pX );
        int y = ::varAsNumeric<int>( pY );

        pResult->vt = VT_UINT;
        pResult->uintVal = m_item->getPixel( x, y );
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::setPixel( VARIANT* pX, VARIANT* pY, VARIANT* pValue )
{
    if ( m_item && ::varIsNumeric( pX ) && ::varIsNumeric( pY ) && ::varIsNumeric( pValue ) )
    {
        int x = ::varAsNumeric<int>( pX );
        int y = ::varAsNumeric<int>( pY );
        int value = ::varAsNumeric<uint32_t>( pValue );

        m_item->setPixel( x, y, value );
    }
    return S_OK;
}

STDMETHODIMP CScriptableImage::rotate( VARIANT* pDegrees )
{
    return S_OK;
}

STDMETHODIMP CScriptableImage::crop( VARIANT* pX, VARIANT* pY, VARIANT* pWidth, VARIANT* pHeight )
{
    return S_OK;
}
